package com.glut.campusrepair.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.glut.campusrepair.entity.FailureRepair1;
import com.glut.campusrepair.service.IFailureRepair1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 故障保修表 前端控制器
 */
@RestController
@RequestMapping("/failure-repair1")
public class FailureRepair1Controller {

    @Autowired
    private IFailureRepair1Service failureRepair1Service;

    /**
     * 根据 ID 获取故障报修信息
     *
     * @param id 故障报修的 ID
     * @return 对应的故障报修信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<FailureRepair1> getFailureRepairById(@PathVariable Integer id) {
        FailureRepair1 repair = failureRepair1Service.getById(id);
        if (repair == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(repair);
    }

    /**
     * 获取所有故障报修记录
     *
     * @return 所有故障报修记录的列表
     */
    @GetMapping("/list")
    public ResponseEntity<List<FailureRepair1>> getAllFailureRepairs() {
        List<FailureRepair1> repairs = failureRepair1Service.list();
        return ResponseEntity.ok(repairs);
    }

    /**
     * 分页获取故障报修记录
     *
     * @param page 页码
     * @param size 每页显示的记录数
     * @param query 搜索关键词（可选）
     * @return 分页后的故障报修记录
     */
    @GetMapping("/listPage")
    public ResponseEntity<IPage<FailureRepair1>> getFailureRepairsByPage(
            @RequestParam int page,
            @RequestParam int size,
            @RequestParam(required = false) String query) {
        Page<FailureRepair1> pageRequest = new Page<>(page, size);
        QueryWrapper<FailureRepair1> queryWrapper = new QueryWrapper<>();

        if (query != null && !query.isEmpty()) {
            queryWrapper.lambda().like(FailureRepair1::getFailureRepairId, query)
                    .or().like(FailureRepair1::getFullName, query);
        }

        IPage<FailureRepair1> resultPage = failureRepair1Service.page(pageRequest, queryWrapper);
        return ResponseEntity.ok(resultPage);
    }

    /**
     * 更新故障报修信息
     *
     * @param failureRepair1 要更新的故障报修实体
     * @return 更新结果
     */
    @PutMapping("/update")
    public ResponseEntity<Boolean> updateFailureRepair(@RequestBody FailureRepair1 failureRepair1) {
        boolean updated = failureRepair1Service.updateById(failureRepair1);
        if (!updated) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(updated);
    }

    /**
     * 审批故障报修
     *
     * @param id 故障报修的 ID
     * @return 审批结果
     */
    @PutMapping("/approve/{id}")
    public ResponseEntity<Boolean> approveFailureRepair(@PathVariable Integer id) {
        FailureRepair1 repair = failureRepair1Service.getById(id);
        if (repair == null) {
            return ResponseEntity.notFound().build();
        }
        repair.setRepairStatus("已审批");
        boolean updated = failureRepair1Service.updateById(repair);
        return ResponseEntity.ok(updated);
    }



    /**
     * 获取所有未处理的故障报修记录
     *
     * @return 未处理的故障报修记录列表
     */
    @GetMapping("/unfinished")
    public ResponseEntity<List<FailureRepair1>> getUnfinishedFailureRepairs() {
        QueryWrapper<FailureRepair1> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("repair_status", "待处理");
        List<FailureRepair1> repairs = failureRepair1Service.list(queryWrapper);
        return ResponseEntity.ok(repairs);
    }





    /**
     * 获取所有已审批的故障报修记录
     *
     * @return 已审批的故障报修记录列表
     */
    @GetMapping("/approved")
    public ResponseEntity<Map<String, Object>> getApprovedFailureRepairs(
            @RequestParam int page,
            @RequestParam int size) {

        Page<FailureRepair1> pageRequest = new Page<>(page, size);
        QueryWrapper<FailureRepair1> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("repair_status", "已审批");

        IPage<FailureRepair1> resultPage = failureRepair1Service.page(pageRequest, queryWrapper);

        Map<String, Object> response = new HashMap<>();
        response.put("records", resultPage.getRecords());
        response.put("total", resultPage.getTotal());

        return ResponseEntity.ok(response);
    }

}
